﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;

namespace SaleSystem.BaseData
{
    public partial class frm_CustomerEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
            //SModel.Cust_BData_Id = ValueHandler.GetIntNumberValue( this.ddl_Cust_BData_Id.SelectedValue );
            //SModel.Cust_Code = this.tb_Cust_Code.Text;
            SModel.Cust_CompanyAddress = this.tb_Cust_CompanyAddress.Text;
            //SModel.Cust_FamilyAddress = this.tb_Cust_FamilyAddress.Text;
            //SModel.Cust_Id = Id;
            //SModel.Cust_IdentityCode = this.tb_Cust_IdentityCode.Text;
            //SModel.Cust_Memo = this.tb_Cust_Memo.Text;
            SModel.Cust_Name = this.tb_Cust_Name.Text;
            //SModel.Cust_Professional = ValueHandler.GetIntNumberValue(this.ddl_Cust_Professional.SelectedValue);
            //SModel.Cust_Sex = ValueHandler.GetIntNumberValue( this.rbl_Cust_Sex.SelectedValue);
            //SModel.Cust_Tel1 = this.tb_Cust_Tel1.Text;
            //SModel.Cust_Tel2 = this.tb_Cust_Tel2.Text;
            //SModel.Email = this.tb_Email.Text;
            //SModel.QQ = this.tb_QQ.Text;
            //SModel.State = 1; 
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("编码或者名称已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
                else
                    ExtAspNet.Alert.Show("客户信息编辑失败！", MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
        }
        private void BindDropDownList()
        {
            BaseDataOp Manager = new BaseDataOp();
            //客户类型
            //DataTable dtList1 = Manager.GetList("BData_Type=2 ");
            //ddl_Cust_BData_Id.DataSource = dtList1;
            //ddl_Cust_BData_Id.DataBind();
            //ddl_Cust_BData_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //职业
            //DataTable dtList2 = Manager.GetList("BData_Type=3 ");
            //ddl_Cust_Professional.DataSource = dtList2;
            //ddl_Cust_Professional.DataBind();
            //ddl_Cust_Professional.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //DataTable dtType = Manager.GetTypeList();
            //ddl_BData_Type.DataSource = dtType;
            //ddl_BData_Type.DataBind();
            //ddl_BData_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
        }
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.Cust_Id = Id;
                SModel = Manager.GetModel(SModel) as Customer;
                this.tb_Cust_Code.Text = SModel.Cust_Code;
                this.tb_Cust_CompanyAddress.Text = SModel.Cust_Tel1;
                //this.tb_Cust_FamilyAddress.Text = SModel.Cust_FamilyAddress;
                //this.tb_Cust_IdentityCode.Text = SModel.Cust_IdentityCode;
                //this.tb_Cust_Memo.Text = SModel.Cust_Memo;
                this.tb_Cust_Name.Text = SModel.Cust_Name;
                //this.tb_Cust_Tel1.Text = SModel.Cust_Tel1;
                //this.tb_Cust_Tel2.Text = SModel.Cust_Tel2;
                //this.tb_Email.Text = SModel.Email;
                //this.tb_QQ.Text = SModel.QQ;
                //this.ddl_Cust_BData_Id.SelectedValue = SModel.Cust_BData_Id.ToString();
                //this.ddl_Cust_Professional.SelectedValue = SModel.Cust_Professional.ToString();
                //this.rbl_Cust_Sex.SelectedValue = SModel.Cust_Sex.ToString();
            }
            else
            {
                this.tb_Cust_Code.Text = Coder.CreateCoder(EMenuList.客户管理);
            }
        }

        #endregion

        #region Property
        CustomerOp Manager = new CustomerOp();
        new Sale_Model.Customer SModel = new Sale_Model.Customer();
        #endregion
    }
}
